<?php
/**
 * Copyright (c) 2006-2007, Julien PORTALIER
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright    Copyright (c) 2006-2007, Julien PORTALIER
 * @link         http://featherphp.googlecode.com/
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Manipulates Javascript contents.
 */
class JavascriptHelper extends Helper
{
	protected $_links   = array();
	protected $_inlines = array();
	
	function link($url)
	{
		if (strpos($url, '://') === false)
		{
			if ($url{0} != '/')
				$url = "js/$url";
			
			if (Core::compress_js)
			{ 
				#$url = "/js.php?url=$url.js";
				$url = "/c/$url.js";
			}
			else
			{
				$url = "/$url.js";
			}
			
			$url .= '?'.Core::version;
		}
		return '<script type="text/javascript" src="'.$url.'"></script>';
	}
	
	function inline($script)
	{
		echo <<<EndToolbarJavascript

<script type="text/javascript">
// <![CDATA[
$script
// ]]>
</script>

EndToolbarJavascript;
	}
	
	function add($url)
	{
		if (!in_array($url, $this->_links))
			$this->_links[] = $url;
	}
	
	function addInline($script)
	{
		$this->_inlines[] = $script;
	}
	
	function links()
	{
		$str = '';
		foreach($this->_links as $url)
			$str .= $this->link($url);
		return $str;
	}
	
	function inlines()
	{
		if (empty($this->_inlines))
			return;
		$str = "\n<script type=\"text/javascript\">\n// <![CDATA[";
		foreach($this->_inlines as $script)
			$str .= "\n$script";
		$str .= "\n// ]]>\n</script>\n";
		return $str;
	}
}
?>